package com.yyz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yyz.common.R;
import com.yyz.domain.Dish;
import com.yyz.dto.DishDto;
import com.yyz.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yyz
 * @create 2023-07-21 15:20
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 添加菜品和口味
     * @param dishDto
     */
    @PostMapping
    public R insert(@RequestBody DishDto dishDto){
        log.info("{}",dishDto);

        dishService.insert(dishDto);

        return R.success("添加成功！");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R pageList(Integer page, Integer pageSize, String name) {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        IPage<DishDto> pageInfo = dishService.pageList(page, pageSize, name);

        return R.success(pageInfo);
    }

    /**
     * 根据id页面回显
     * @param ids
     * @return
     */
    @GetMapping("/{ids}")
    public R selectById(@PathVariable Long ids){
        Dish dish = dishService.selectById(ids);

        return R.success(dish);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        log.info("{}",dishDto);

        dishService.update(dishDto);

        return R.success("修改成功！");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(Long[] ids){
        log.info("{}",ids);

        dishService.delete(ids);

        return R.success("删除成功");
    }

    /**
     * 停售 启售
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable Integer status, Long[] ids){
        log.info("{}",ids);

        dishService.status(status,ids);

        return R.success("操作成功！");
    }

    /**
     * 查询菜品分类下的菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        log.info("{}",categoryId);

        List<DishDto> dishes = dishService.list(categoryId);

        return R.success(dishes);
    }
}
